<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${base}/resources/js/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
<link href="${base}/resources/css/common.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${base}/resources/js/lib/jquery.1.11.3.min.js"></script>
<script type="text/javascript" src="${base}/resources/js/lib/zTree_v3/js/jquery.ztree.all-3.5.js"></script>
<script type="text/javascript" src="${base}/resources/js/lib/zTree_v3/js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="${base}/resources/js/lib/zTree_v3/js/jquery.ztree.exedit-3.5.js"></script>
<script type="text/javascript">
var zTreeObj;
var currentNode;
$(document).ready(function(){
	function loadTree(){
		$.ajax({
			url:"${base}/admin/department/tree",
			success:function(result){
				$("#tree").html(result);
				initTree(treeClick);
			}
		});
	}
	
	loadTree();
	
	/**
	 * 添加部门
	 */
	$("div#depart").on("click","input.saveAdd",function(){
		$("table#addTable").find("label.errMsg").empty();
		var companyId=$("table#addTable").find("input.companyId").val();
		var parentId=$("table#addTable").find("input.parentId").val();
		var name=$(this).closest("tr").find("input.addName").val();
		var code=$(this).closest("tr").find("input.addCode").val();
		var orderNum=$(this).closest("tr").find("input.addOrderNum").val();
		if(name=="" || name==null){
			$("table#addTable").find("label.errMsg").html('<font color="red">名称不能为空</font>');
			return false;
		}
		if(code=="" || code==null){
			$("table#addTable").find("label.errMsg").html('<font color="red">代码不能为空</font>');
			return false;
		}
		if(orderNum!="" && orderNum!=null){
			var regInt=/^[0-9]+$/g;
			if(!regInt.test(orderNum)){
				$("table#addTable").find("label.errMsg").html('<font color="red">排序号不是整数</font>');
				return false;
			}
		}
		$.ajax({
			    url: "${base}/admin/department/save",
			    type: "POST",
			    dataType: "json",
			    data: {companyId: companyId,
			           parentId: parentId,
			           name: name,
			           code: code,
			           orderNum: orderNum
			          },
			    success: function(data){
			        	   if(data.success){
			        		   $("table#addTable").find("label.errMsg").html('<font color="blue">'+data.msg+'</font>');  
			        	       setTimeout(function(){
			        	    	   loadTree();
			        	    	   treeClick(zTreeObj,currentNode);
			        	       }, 400);
			        	   }else{
			        		   $("table#addTable").find("label.errMsg").html('<font color="red">'+data.msg+'</font>');
			   				   return false; 
			        	   }
			             },
			    error: function(data){}
		});
	});
	
	/**
	 * 编辑按钮
	 */
	$("div#depart").on("click","a.edit",function(){
		var editRow=$(this).closest("tr");
		editRow.find("a.saveEdit").show();
		editRow.find("a.cancelEdit").show();
		editRow.find("a.delete").hide();
		editRow.find("input.editCode").val(editRow.find("label.code").html());
		editRow.find("input.editName").val(editRow.find("label.name").html());
		editRow.find("input.editOrderNum").val(editRow.find("label.orderNum").html());
		editRow.find("label.code").hide();
		editRow.find("input.editCode").show();
		editRow.find("label.name").hide();
		editRow.find("input.editName").show();
		editRow.find("label.orderNum").hide();
		editRow.find("input.editOrderNum").show();
		$(this).hide();
	});
	
	/**
	 * 取消编辑
	 */
	$("div#depart").on("click","a.cancelEdit",function(){
		var editRow=$(this).closest("tr");
		editRow.find("a.edit").show();
		editRow.find("a.delete").show();
		editRow.find("a.saveEdit").hide();
		editRow.find("label.code").show();
		editRow.find("input.editCode").hide();
		editRow.find("label.name").show();
		editRow.find("input.editName").hide();
		editRow.find("label.orderNum").show();
		editRow.find("input.editOrderNum").hide();
		$(this).hide();
	});
	
	/**
	 * 保存编辑结果
	 */
	$("div#depart").on("click","a.saveEdit",function(){
		var editRow=$(this).closest("tr");
		$("table#addTable").find("label.errMsg").empty();
		var companyId=$("table#addTable").find("input.companyId").val();
		var parentId=$("table#addTable").find("input.parentId").val();
		var id=$(this).attr("dataId");
		var editCode=editRow.find("input.editCode").val();
		var editName=editRow.find("input.editName").val();
		var editOrderNum=editRow.find("input.editOrderNum").val();
		if(editName=="" || editName==null){
			$("table#addTable").find("label.errMsg").html('<font color="red">名称不能为空</font>');
			return false;
		}
		if(editCode=="" || editCode==null){
			$("table#addTable").find("label.errMsg").html('<font color="red">代码不能为空</font>');
			return false;
		}
		if(editOrderNum!="" && editOrderNum!=null){
			var regInt=/^[0-9]+$/g;
			if(!regInt.test(editOrderNum)){
				$("table#addTable").find("label.errMsg").html('<font color="red">排序号不是整数</font>');
				return false;
			}
		}
		$.ajax({
			    url: "${base}/admin/department/save",
			    type: "POST",
			    dataType: "json",
			    data: {id: id,
			           companyId: companyId,
			           parentId: parentId,
			           code: editCode,
			           name: editName,
			           orderNum: editOrderNum
			          },
			    success: function(data){
			        	   if(data.success){
			        		   $("table#addTable").find("label.errMsg").html('<font color="blue">'+data.msg+'</font>');
			        	       var dept=data.dept;
			        	       editRow.find("label.code").html(dept.code);
			        	       editRow.find("label.name").html(dept.name);
			        	       editRow.find("label.orderNum").html(dept.orderNumStr);
			        	       editRow.find("label.code").show();
			        	       editRow.find("label.name").show();
			        	       editRow.find("label.orderNum").show();
			        	       editRow.find("input.editCode").hide();
			        	       editRow.find("input.editName").hide();
			        	       editRow.find("input.editOrderNum").hide();
			        	       editRow.find("label.createDate").html(dept.createDateStr);
			        	       editRow.find("label.modifyDate").html(dept.modifyDateStr);
			        	       editRow.find("a.edit").show();
			        	       editRow.find("a.delete").show();
			        	       editRow.find("a.saveEdit").hide();
			        	       editRow.find("a.cancelEdit").hide();
			        	   }else{
			        		   $("table#addTable").find("label.errMsg").html('<font color="red">'+data.msg+'</font>');
			        	   }
			             },
			    error: function(data){}
		});
	});
	
	/**
	 * 删除部门
	 */
	$("div#depart").on("click","a.delete",function(){
		var deptId=$(this).attr("dataId");
		$.ajax({
			    url: "${base}/admin/department/delete",
			    type: "POST",
			    dataType: "json",
			    data: {deptId: deptId},
			    success: function(data){
			    	       if(data.success){
			    	    	   $("table#addTable").find("label.errMsg").html('<font color="blue">'+data.msg+'</font>');
			    	    	   setTimeout(function(){
			        	    	   loadTree();
			        	    	   treeClick(zTreeObj,currentNode);
			        	       }, 400);
			    	       }else{
			    	    	   $("table#addTable").find("label.errMsg").html('<font color="red">'+data.msg+'</font>');
			    	       }
			             },
			    error: function(data){}
		});
	});
	
});


function treeClick(treeId,node){
	currentNode=node;
	var deptId=null;
	if(node.level!=0){
		deptId=node.id;
	}
	$.ajax({
		    url: "${base}/admin/department/list",
		    type: "POST",
		    data: {deptId: deptId},
		    success: function(data){
		               $("div#depart").html(data);
	                 }
	});
}

function initTree(onClick){
	var setting = {
		async : {
			enable: true,
			url: "${base}/admin/department/loadNext",
			autoParam: ["id"]
		},
		callback:{
			onClick:function(event,treeId,treeNode,clickFlag){
				if (onClick){
					onClick(zTreeObj,treeNode);
				}
			}
		}
	};
	
	var zNodes = [
	{
		id:"${company.id}",
		name:"${company.name}",
		isParent: true
	}
	];
	zTreeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
}
</script>
</head>
<body>
<div id="tree" class="ztree" style="width: 25%; float: left;">
</div>
<div id="depart" style="width: 74%; float: left;">
</div>
</body>
</html>